//
//  ForgetPasswordViewController.swift
//  AnJiBei
//
//  Created by 赵鑫 on 15/10/31.
//  Copyright © 2015年 赵鑫. All rights reserved.
//

import UIKit

class ForgetPasswordViewController: BaseViewController {
    
    
    @IBOutlet weak var userPhoneTextField: UITextField!
    
    @IBOutlet weak var userVerCodeTextField: UITextField!
    
    @IBOutlet weak var getCodeButton: CountDownButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
    /**********按钮操作*************/
    //获取验证码
    @IBAction func getVerCodeButton(sender: AnyObject) {
        
        if userPhoneTextField.text!.isEmpty {
            
            disPlayMessage("还没有输入手机号吖")
        }
            
        else {
            getCodeButton.maxSecond = 30
            getCodeButton.countdown = true
            
            //获取验证码
            SMSSDK.getVerificationCodeByMethod(SMSGetCodeMethodSMS, phoneNumber: userPhoneTextField.text!, zone: "86", customIdentifier: nil, result: {(error) -> Void in
                if error != nil {
                    
                    debugPrint(error)
                    
                } else {
                    
                    PrintUntils.printLog("获取", message: "获取验证码成功")
                } })
            
            
        }
        
        
    }
    
    //提交按钮
    @IBAction func resultCheckButton(sender: UIButton) {
        
        checkPhone()
    }
    
    //返回
    @IBAction func returnBack(sender: UIBarButtonItem) {
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    /**************方法判断**********/
    
    //手机号检测
    func checkPhone() {
    
        if !RegularExpressionsUtils.regularExpressionPhone(userPhoneTextField.text!){
        
            disPlayMessage("输入的手机号不正确，请重新输入")
        }
        else {
        
            if userVerCodeTextField.text!.isEmpty {
            
                disPlayMessage("验证码为空，请输入验证码")
            }
            else {
            
                checkVercode()

            }
            
        }
    }
    
    //验证码验证
    func checkVercode() {
        
      //  var isOk = false
        
        SMSSDK.commitVerificationCode(userVerCodeTextField.text!, phoneNumber: userPhoneTextField.text!, zone: "86", result: {(error) -> Void in
            //isOk = true
            
            if error != nil {
            
                self.disPlayMessage("验证输入有误，请从新输入")
           
            }
            
            else {
            
                self.performSegueWithIdentifier("Forget2NewWord", sender: self)

                
            }
        })
        
      //  return isOk
        
    }
    


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        
        let dev = segue.destinationViewController as! NewPasswordViewController
        
        dev.userPhone = userPhoneTextField.text!
    }


}
